package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.OrderOperation;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgOrderOperationMapper;
import com.ygqh.baby.po.YgOrderOperation;
import com.ygqh.baby.po.YgOrderOperationExample;
import com.ygqh.baby.po.YgOrderOperationExample.Criteria;
import com.ygqh.baby.service.YgOrderOperationService;

@Service
public class YgOrderOperationServiceImpl implements YgOrderOperationService {

	@Autowired
	private YgOrderOperationMapper ygOrderOperationMapper;

	@Override
	public List<YgOrderOperation> find() {
		YgOrderOperationExample example = new YgOrderOperationExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygOrderOperationMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgOrderOperationExample example = new YgOrderOperationExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygOrderOperationMapper.countByExample(example);
	}

	@Override
	public YgOrderOperation findById(Long id) {
		return ygOrderOperationMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgOrderOperation ygOrderOperation) {
		return ygOrderOperationMapper.insertSelective(ygOrderOperation);
	}

	@Override
	public int update(YgOrderOperation ygOrderOperation) {
		return ygOrderOperationMapper.updateByPrimaryKeySelective(ygOrderOperation);
	}

	@Override
	public ResultSet<YgOrderOperation> search(QueryInfo queryInfo, String q) {
		List<YgOrderOperation> list = ygOrderOperationMapper.selectSuper(queryInfo, q);
		int count = ygOrderOperationMapper.countSuper(q);
		ResultSet<YgOrderOperation> resultSet = new ResultSet<YgOrderOperation>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgOrderOperationExample example = new YgOrderOperationExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgOrderOperation record = new YgOrderOperation();
		record.setStatus(DataStatus.Delete);

		return ygOrderOperationMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgOrderOperation ygOrderOperation) {
		int r = 0;
		if (ygOrderOperation.getId() != null) {
			r = this.update(ygOrderOperation);
		} else {
			r = this.save(ygOrderOperation);
		}
		return r;
	}

	@Override
	public List<YgOrderOperation> findByOrderId(Long orderId, OrderOperation operation) {
		YgOrderOperationExample example = new YgOrderOperationExample();
		Criteria criteria = example.createCriteria().andOrderIdEqualTo(orderId)
				.andStatusEqualTo(DataStatus.Valid.name());
		if (operation != null) {
			criteria.andOperationTypeEqualTo(operation.name());
		}
		return ygOrderOperationMapper.selectByExample(example);
	}

}
